package handler

import (
	"git.oschina.net/fanbuchi/redpack/constant"
	"git.oschina.net/fanbuchi/redpack/domain"
	"git.oschina.net/fanbuchi/redpack/errors"
	"git.oschina.net/fanbuchi/redpack/service"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"strconv"
)


type AccountHandler struct {
	*service.AccountService
	*service.ProfileService
}


// @Summary 创建用户账号
// @Description 创建一个用户账号
// @Tags account
// @Accept  json
// @Produce  json
// @Param   account     body    domain.LoginReq     true        "账户信息"
// @Param   Authorization     header    string     true        "权限认证" default(Basic Y2xpZW50OnNlY3JldA==)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/ [post]
func  (handler*AccountHandler)Create(c *gin.Context)  {
	var params domain.Account
	if err := c.ShouldBindBodyWith(&params, binding.JSON); err != nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	result,err:=handler.AccountService.Create(&params)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	_,err=handler.ProfileService.Create(&domain.Profile{AccountId:result.ID,NickName:result.UserName})
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}




// @Summary 删除账号
// @Description 删除一个用户的账号
// @Tags account
// @Accept  json
// @Produce  json
// @Param    id    query    int     true        "账户id" default(1)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/delete [delete]
func  (handler*AccountHandler)Delete(c *gin.Context)  {
	var params domain.Account
	if err := c.ShouldBindQuery(&params);err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}

	result,err:=handler.AccountService.Delete1ById(params.ID)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	//账户
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Msg:"账号"+result.UserName+"已删除"})
}

var secrets = gin.H{
	"client": gin.H{"email": "austin@example.com", "phone": "666"},
}
// @Summary 查询账号
// @Description QueryById 查询一个用户的账号
// @Tags account
// @Accept  json
// @Produce  json
// @Param    id    query    int     true        "账户id" default(1)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/query [get]
func  (handler*AccountHandler)QueryById(c *gin.Context)  {
	id,has := c.GetQuery("id")
	params,err:=strconv.ParseInt(id,10,64)
	if !has {
		c.JSON(http.StatusOK, domain.Rsp{Status: http.StatusBadRequest, Msg: http.StatusText(http.StatusBadRequest)})
		return
	}

	result,err:=handler.AccountService.Query1ById(params)

	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	if result==nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:_error.ErrAccountNotExist.Error()})
		return
	}
	accountId:=c.GetInt64(constant.AccountId)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能查詢其他人的信息
	if (accountRole==domain.ROLE_USER || accountRole==domain.ROLE_SUPER_USER) &&
		result.ID!=accountId{
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg: _error.ErrAccountOnlyYou.Error()})
		return
	}
	//密码
	result.Password=""
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}


// @Summary 查询账号
// @Description QueryByUserName 查询一个用户的账号
// @Tags account
// @Accept  json
// @Produce  json
// @Param    userName    query    string     true        "userName" default(zhangsan)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/queryByUserName [get]
func  (handler*AccountHandler)QueryByUserName(c *gin.Context)  {
	userName := c.Query("userName")
result,err:=handler.AccountService.Query1ByUserName(userName)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	accountId:=c.GetInt64(constant.AccountId)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能查詢其他人的信息
	if (accountRole==domain.ROLE_USER || accountRole==domain.ROLE_SUPER_USER) &&
		result.ID!=accountId{
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:_error.ErrAccountOnlyYou.Error()})
		return
	}
	//密码
	result.Password=""
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}

// @Summary 查询账号
// @Description QueryByDomain 查询一个用户的账号
// @Tags account
// @Accept  json
// @Produce  json
// @Param   account     body    domain.AccountReq     true        "查询结构条件"
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/queryByDomain [post]
func  (handler*AccountHandler)QueryByDomain(c *gin.Context)  {
	var accountReq domain.AccountReq
	err := c.ShouldBindBodyWith(&accountReq,binding.JSON)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	params:=domain.Account{UserName:accountReq.UserName,
		Role:accountReq.Role}
	accountId:=c.GetInt64(constant.AccountId)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能查詢其他人的信息
	if accountRole==domain.ROLE_ADMIN{
		params.ID=accountReq.ID
	}else{
		params.ID=accountId
	}
	result,err:=handler.AccountService.Query1ByDomain(&params)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusForbidden,Msg:err.Error()})
		return
	}
	//密码
	if result!=nil {
		result.Password=""
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:result})
}


// @Summary 查询用户列表
// @Description search 查询用户列表
// @Tags account
// @Accept  json
// @Produce  json
// @Param   account     body    domain.AccountReq     true        "查询结构条件"
// @Param   page     query    int     true        "page" default(1)
// @Param   size     query    int     true        "size" default(2)
// @Param   Authorization     header    string     true        "权限认证" default(Bearer 3bbea1b6-f8e4-4da9-a9f3-0413abff47c5)
// @Success 200 {object} domain.Account    "调用成功"
// @Failure 400 {object} domain.Rsp "失败，参数错误!"
// @Router /account/search [post]
func  (handler*AccountHandler)Search(c *gin.Context)  {
	page:= c.DefaultQuery("page","1")
	size:= c.DefaultQuery("size","18")
	//params,err:=strconv.ParseInt(id,10,64)
	var accountReq domain.AccountReq
	err := c.ShouldBindBodyWith(&accountReq,binding.JSON)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	params:=domain.Account{UserName:accountReq.UserName,
		Role:accountReq.Role}
	accountId:=c.GetInt64(constant.AccountId)
	accountRole:=c.GetString(constant.AccountRole)
	//普通用戶不能查詢其他人的信息
	if accountRole==domain.ROLE_ADMIN{
		params.ID=accountReq.ID
	}else{
		params.ID=accountId
	}
	_page,_:=strconv.Atoi(page)
	_size,_:=strconv.Atoi(size)
	ret,err:=handler.AccountService.Search(&params,_page,_size)
	if err!=nil {
		c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusBadRequest,Msg:err.Error()})
		return
	}
	c.JSON(http.StatusOK, domain.Rsp{Status:http.StatusOK,Data:ret})
}